package com.stone.serverless.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.stone.serverless.datasource.DataSourceService;
import com.stone.serverless.datasource.ExposeServiceManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Description: TODO
 * @Author: MECHREVO
 * @Date: 2022/1/14 17:45
 * @Version: 1.0.0
 */
@Configuration
public class DataSourceConfig implements DataSourceService {

    @Value("${spring.datasource.url}")
    public String url;

    @Value("${spring.datasource.username}")
    public String username;

    @Value("${spring.datasource.password}")
    public String password;


    @Override
    @Bean
    public DataSource dataSource() {
        DruidDataSource ds = new DruidDataSource();
        // 基本信息
        ds.setUrl(url);
        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds.setUsername(username);
        ds.setPassword(password);
       // ds.setConnectionErrorRetryAttempts(0);
        ExposeServiceManager.INSTANCE.add(this);
        return ds;
    }
    //
    //public DataSource getDs() {
    //    DataSource dataSource = dataSource();
    //    return dataSource;
    //}
}
